 //
//  ColumnSortingViewController.m
//  SuperRadio
//
//  Created by Damon on 14/12/17.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "ColumnSortingViewController.h"
#import "Classify.h"
#import "ClassifyOfList.h"
#import "ClassifyCell.h"
#import "UIImageView+WebCache.h"
#import "NetHandler.h"
#import "Categories.h"
#import "AlbumOfListViewController.h"

@interface ColumnSortingViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) UICollectionView *collectionView;
// 屏幕的高
@property (nonatomic, assign) CGFloat height;
// 屏幕的宽
@property (nonatomic, assign) CGFloat width;
@end

@implementation ColumnSortingViewController

// 分类界面的初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.width = [UIScreen mainScreen].bounds.size.width;
        self.height = [UIScreen mainScreen].bounds.size.height;
    }
    
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self handleData];
    
    
}

// 数据处理
- (void)handleData
{
    self.arr = [NSMutableArray array];

    NSString *path = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_tag_list?category=%@&device=iPhone&type=album", self.categories.name];
    [NetHandler getDataWithURL:path completion:^(NSData *data) {
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *classifyDic = [NSDictionary dictionaryWithDictionary:result];
        
        NSArray *tempArr = [NSArray arrayWithArray:[classifyDic objectForKey:@"list"]];
        
        for (NSDictionary * dic in tempArr) {
            
            ClassifyOfList *classifyOfList = [[ClassifyOfList alloc] initWithDictionary:dic];
            
            [self.arr addObject:classifyOfList];
            [classifyOfList release];
        }
        
        
        [self.collectionView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    // 隐藏导航栏返回按钮
    //self.navigationItem.hidesBackButton = YES;
    self.navigationItem.title = self.categories.title;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.width - 40) / 3, (self.width - 40) / 3);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    // 定义UICollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height - 10) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    // 指定代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    [self.view addSubview:self.collectionView];
    
    
    // 注册 collectionViewCell类
    [self.collectionView registerClass:[ClassifyCell class] forCellWithReuseIdentifier:@"classify"];
    [_collectionView release];
    [flowLayout release];
    
    
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    backImage.image = [UIImage imageWithContentsOfFile:path];
    backImage.alpha = 0.5;
    
    self.collectionView.backgroundView = backImage;
    
}

// 导航栏按钮点击

- (void)clicked:(UIBarButtonSystemItem *)barButton
{
    AlbumOfListViewController * albumOfListVC = [[AlbumOfListViewController alloc] init];
    [self.navigationController pushViewController:albumOfListVC animated:YES];
    
    [albumOfListVC release];
}

// section的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.arr.count;
    
}

// 自定义CollectionViewCell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"classify" forIndexPath:indexPath];
    ClassifyOfList *list = [self.arr objectAtIndex:indexPath.item];
    cell.classifyOfList = list;
    cell.backgroundColor = [UIColor whiteColor];
    
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumOfListViewController * albumOfListVC = [[AlbumOfListViewController alloc] init];
   
    albumOfListVC.classifyOflist = [self.arr objectAtIndex:indexPath.item];
    albumOfListVC.categories = self.categories;
    [self.navigationController pushViewController:albumOfListVC animated:YES];
    
    [albumOfListVC release];

}

- (void)dealloc
{
    [_arr release];
    [_collectionView release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
